package com.akl.ssmspringboot.service.impl;

import com.akl.ssmspringboot.dao.UserMapper;
import com.akl.ssmspringboot.entity.User;
import com.akl.ssmspringboot.service.UserService;
import com.akl.ssmspringboot.utils.PageResult;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName:
 * @Author: Guoxy
 * @Date: Created in 0:21 2018/11/7
 * @Version:1.0
 */
@Service
@Transactional
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public PageResult<User> pageFind(int currPage, int pageSize, User user) {
        List<User> userList = userMapper.pageFind(currPage, pageSize, user);
        int totalCount = userMapper.totalCount(user);
        PageResult<User> pageResult = new PageResult<>(currPage, pageSize, totalCount, userList);
        return pageResult;
    }

    @Override
    public int addUser(User user) {
        int userId = userMapper.addUser(user);
        return userId;
    }

    @Override
    public void delUser(int userId) {
        userMapper.delUser(userId);
    }

    @Override
    public void updateUser(User user) {
        userMapper.updateUser(user);
    }

    @Override
    public User findUserById(int userId) {
        User user = userMapper.selectUserById(userId);
        return user;
    }

    @Override
    public Boolean checkUsername(String username) {
        User user = userMapper.selectUserByName(username);
        Boolean result = false;
        if (user == null) {
            result = true;
        }
        return result;
    }

    @Override
    public Boolean checkUptUsername(Integer userId, String username) {
        User userByName = userMapper.selectUserByName(username);
        User userById = userMapper.selectUserById(userId);
        Boolean result = false;
        //此用户名与原名一样
        if (userByName != null && userByName.equals(userById)) {
            result = true;
        }
        //未找到此用户名
        if (userByName == null) {
            result = true;
        }
        return result;
    }

    @Override
    public List<User> findAll() {
        User user = new User();
        int totalCount = userMapper.totalCount(user);
        List<User> list = userMapper.pageFind(1, totalCount, user);
        return list;
    }

}
